package com.heima.redisson.controller;

import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/{time}")
    public ResponseEntity send(@PathVariable("time") int time) {
        // 发送消息到redis
        // 需要使用redisson的客户端
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("test-dqueue");
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        delayedQueue.offer("hello", time, TimeUnit.SECONDS);
        System.out.println("发送消息完成,延迟时间为: " + time +" 发送时间为: "+new Date());
        return ResponseEntity.ok("done");
    }
}